package com.xs.ahoi.hcxs.model.enums;


import java.util.Arrays;

/**
 * TTS语速枚举
 *
 * @author fuhuichao
 * @date 2020-01-17 10:55
 */
public enum SpeedEnum {

    VOICE_SPEED_VERY_SLOW(-2, "0.6倍语速"),
    VOICE_SPEED_SLOWDOWN(-1, "0.8倍语速"),
    VOICE_SPEED_NORMAL(0, "正常语速"),
    VOICE_SPEED_ACCELERATE(1, "1.2倍语速"),
    VOICE_SPEED_VERY_FAST(2, "1.5倍语速");;

    private int code;
    private String text;

    SpeedEnum(int code, String text) {
        this.code = code;
        this.text = text;
    }

    public int getCode() {
        return code;
    }

    public String getText() {
        return text;
    }

    public static SpeedEnum getNextSpeed(int code, boolean next) {
        for (int i = 0; i < SpeedEnum.values().length; i++) {
            SpeedEnum item = SpeedEnum.values()[i];
            if (item.getCode() == code) {
                if (next) {
                    if (i == SpeedEnum.values().length - 1) {
                        return item;
                    }
                    return SpeedEnum.values()[i + 1];
                } else {
                    if (i == 0) {
                        return item;
                    }
                    return SpeedEnum.values()[i - 1];
                }
            }
        }
        return VOICE_SPEED_NORMAL;
    }

    public static SpeedEnum getByCode(int code) {
        return Arrays.stream(SpeedEnum.values()).filter(o -> o.getCode() == code).findFirst().orElse(VOICE_SPEED_NORMAL);
    }

    public static SpeedEnum getByText(String text) {
        return Arrays.stream(SpeedEnum.values()).filter(o -> o.getText().equals(text)).findFirst().orElse(VOICE_SPEED_NORMAL);
    }
}
